<?php

namespace Mouta\AmistateBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Mouta\AmistateBundle\utils\Strings;
use Mouta\AmistateBundle\utils\SfSolr;

class AdminUsuarioController extends Controller
{
    public function listAction($page = 1)
    {
        $filtros = $this->procesarFiltro();
        $limit = 40;
        $offset = ($page-1) * $limit;
        $em = $this->getDoctrine()->getEntityManager();
        $result = $em->getRepository('AmistateBundle:Usuario')->findAllUser($filtros,$limit,$offset);
        $usuarios = $result['usuarios'];
        $cant = $result['cant'];
        
        $cant_pag = ceil($cant/$limit);
        $url_base = $this->get('router')->generate('_adminUsuarioList');
        return $this->render('AmistateBundle:adminUsuario:list.html.twig',array(
            'usuarios'=>$usuarios,
            'cant'=>$cant,
            'page'=>$page,
            'url_base'=>$url_base,
            'cant_pag'=>$cant_pag,
            'tags'=>array(),
            'filtros'=>$filtros,
            ));
    }
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();
        /* @var $user \Mouta\AmistateBundle\Entity\Usuario */
        $user = $em->getRepository('AmistateBundle:Usuario')->getFullUserById($id);
        if($this->getRequest()->getMethod()=="POST"){
            $posts = $this->getRequest()->request->all();
            foreach($posts as $key => $value){
                $key = str_replace('control_', '', $key);
                $key = str_replace('u_', '', $key);
                if($key == 'tab' || $key == 'rclave' || $key == 'verificado')
                    continue;
                if(strpos($key, 'image_') === 0 )
                    continue;
                
                if($key=='cumple')
                    $value = date_create_from_format('d/m/Y', $value);
                $prop = "set".Strings::camelize($key);
                if (method_exists($user, $prop)) {
                    $user->$prop($value);
                }
            }
            if(isset($posts['control_verificado']))
                $user->setVerificado(true);
            else
                $user->setVerificado(false);
            $now = date_create_from_format('d/m/Y', date('d/m/Y'));
            
            if($this->getRequest()->get('rehabilitar')){
                $user->setBorrado(0);
                $user->setDeletedAt(null);
            }
            if($this->getRequest()->get('eliminar')){
                $user->setBorrado(1);
                $user->setDeletedAt($now);
            }
            if($this->getRequest()->get('validar')){
                $user->setVerificado(true);
            }
            
            $this->procesarFotos($user);

            $em->getRepository('AmistateBundle:Usuario')->SaveUser($user);
            $em->getRepository('AmistateBundle:ImagenUsuario')->actualizarDefault($user->getId());
            
            if($this->getRequest()->get('validar')){
                $idNext = $em->getRepository('AmistateBundle:Usuario')->findUserSinValidar();
                if($idNext)
                    return $this->redirect( $this->generateUrl('_adminUsuarioEditar',array('id'=>$idNext)));
                else
                    return $this->redirect( $this->generateUrl('_adminUsuarioList'));
            }
            
            
            return $this->redirect( $this->generateUrl('_adminUsuarioEditar',array('id'=>$id)));
        }
        return $this->render('AmistateBundle:adminUsuario:edit.html.twig',array(
            'user'=>$user,
            ));
    }
}
